<cfset mySelf = viewState.getValue("mySelf") />
<cfset resourceUrl = viewstate.getValue("resourceUrl")>
<cfset resourceHistory = viewstate.getValue("resourceHistory")>
<cfset currentUser = viewstate.getValue("currentUser")>

<cfset PageTitle="cfdiff Subversion Browser">
<cfset StyleSheet="cfdiff.css">
<cfset DiffGraphic='<img src="diff.png" width="16" width="16" alt="View the difference between this file and the previous version" border="0" />'>
<!--- We don't want to provide the ability to diff everything, just certain file types --->
<cfset Diffable="cfc,cfm,cfml,txt,plx,php,php4,php5,asp,aspx,xml,html,htm,sql,css,js">


<cffunction name="FreshnessRating" returntype="string" output="false">
	<cfargument name="Updated" type="any" required="true">
	<cfset var Age=99>
	<cfif IsDate(Arguments.Updated)>
		<cfset Age=DateDiff("d",Arguments.Updated,Now())>
	</cfif>
	<cfif Age LTE 2><cfreturn "smokin">
	<cfelseif Age LTE 5><cfreturn "hot">
	<cfelseif Age LTE 10><cfreturn "fresh">
	<cfelseif Age LTE 30><cfreturn "fine">
	</cfif>
	<cfreturn "aged">
</cffunction>

	<!--- Show our generic file list or history list --->
<cfset IsDir=False>
<!--- Request URLs will look like: /svn.cfm/org/rickosborne/diff.cfc:12:25 --->
<cfset FilePath=REReplace(REReplace(resourceUrl,"[.][.]+",".","ALL"),"[/][/]+","/","ALL")>
<cfif FilePath CONTAINS ":">
	<!--- There is at least one revision number given --->
	<cfset Version=ListRest(FilePath,":")>
	<cfif Version CONTAINS ":">
		<!--- There's a left/right pair of revision numbers --->
		<cfset PrevVersion=ListFirst(Version,":")>
		<cfset Version=ListRest(Version,":")>
		<cfif Right(Version,1) EQ "f">
			<cfset FullDiff=true>
			<cfset Version=Val(Version)>
		</cfif>
	</cfif>
	<cfset FilePath=ListFirst(FilePath,":")>
</cfif>

<cfif FilePath EQ "">
	<cfset FilePath="/">
</cfif>
<cfif Right(FilePath,1) EQ "/">
	<cfset IsDir=True>
</cfif>
<cfset TotalBytes=0>
<cfset TotalFiles=0>
<cfset TotalDirs=0>
<cfset EvenOdd=ListToArray("even,odd")>
<cfset IsDiff=false>
<cfoutput>
<strong>#filePath#</strong>
<table border="0" width="100%" class="list" id="revTable">
	<thead>
	<tr>
	<cfif IsDir><th align="left">Name</th></cfif>
	<th align="right">Revision</th>
	<cfif NOT IsDir><th align="center">Diff</th></cfif>
	<cfif IsDir><th align="right">Size</th></cfif>
	<th align="center">Date</th>
	<th align="left">Author</th>
	<cfif NOT IsDir><th align="left">Message</th></cfif>
	</tr>
	</thead>
	<tbody>
</cfoutput>
<cfoutput query="resourceHistory">
	<cfif IsNumeric(Size)><cfset TotalBytes=TotalBytes+Size></cfif>
	<cfset FileExt=LCase(ListLast(Name,"."))>
	<cfset CanDiff=false>
	<cfif Kind EQ "file">
	<cfset TotalFiles=TotalFiles+1>
	<cfif ListFindNoCase(Diffable,FileExt) GT 0>
		<cfset CanDiff=true>
	</cfif>
	<cfelseif Kind EQ "dir">
		<cfset TotalDirs=TotalDirs+1>
	</cfif>
	<tr class="#EvenOdd[IncrementValue(CurrentRow MOD 2)]#" valign="top">
		<cfif IsDir><td><a href="#myself##filePath#<cfif Kind EQ 'dir'>/</cfif>">#HTMLEditFormat(Name)#</a></td></cfif>
		<td nowrap="nowrap" class="num">
			<cfif IsDir>#NumberFormat(Revision)#<cfelse><a href="#myself##filepath#:#NumberFormat(Revision)#">#NumberFormat(Revision)#</a></cfif>
		</td>
		<cfif NOT IsDir><td align="center"><cfif CanDiff AND (CurrentRow LT RecordCount)><a href="#CGI.SCRIPT_NAME##Path#:#resourceHistory.Revision[IncrementValue(CurrentRow)]#:#Revision#">#DiffGraphic#</a><cfelse>&nbsp;</cfif></td></cfif>
		<cfif IsDir><td nowrap="nowrap" class="num"><cfif (Kind EQ 'file') AND IsNumeric(Size)>#NumberFormat(Size)#</cfif></td></cfif>
		<td class="date<cfif IsDate(Date)> #FreshnessRating(Date)#</cfif>" nowrap="nowrap"><cfif IsDate(Date)>#DateFormat(Date,"yyyy-mm-dd")# #TimeFormat(Date,"HH:mm:ss")#<cfelse>#HTMLEditFormat(Date)#</cfif></td>
		<td>#HTMLEditFormat(Author)#</td>
		<cfif NOT IsDir><td>#HTMLEditFormat(Message)#</td></cfif>
	</tr>
</cfoutput>
<cfoutput>
	</tbody>
	<tfoot>
	<tr>
		<cfif IsDir>
		<td colspan="5">#NumberFormat(TotalBytes)# byte<cfif TotalBytes NEQ 1>s</cfif> in <cfif TotalFiles GT 0>#NumberFormat(TotalFiles)# file<cfif TotalFiles NEQ 1>s</cfif><cfif TotalDirs GT 0> and </cfif></cfif><cfif TotalDirs GT 0>#NumberFormat(TotalDirs)# director<cfif TotalDirs NEQ 1>ies<cfelse>y</cfif></cfif>.</td>
		<cfelse>
		<td colspan="5">#resourceHistory.RecordCount# revision<cfif resourceHistory.RecordCount NEQ 1>s</cfif> found.</td>
		</cfif>
	</tr>
	</tfoot>
</table>
</cfoutput>